/**
 * 将base64图片数据转换成Blob
 * @param image
 * @returns {Blob}
 */
const base64toBlob = (image) => {
	const bytes = window.atob(image.split(',')[1])
	const array = []
	for (let i = 0; i < bytes.length; i++) {
		array.push(bytes.charCodeAt(i))
	}
	return new Blob([new Uint8Array(array)], {
		type: 'image/png'
	})
}

/*
 * 创建时间和现在时间作对比
 * @param time 时间戳
 * 
 * */
const beforeTime = (time) => {
    let way = Date.parse(new Date()) / 1000 - time;
    let r = '';
    if (way < 60) {
        r = '刚刚';
    } else if (way >= 60 && way < 3600) {
        r = Math.floor(way / 60) + '分钟前';
    } else if (way >= 3600 && way < 86400) {
        r = Math.floor(way / 3600) + '小时前';
    } else if (way >= 86400 && way < 2592000) {
        r = Math.floor(way / 86400) + '天前';
    } else if (way >= 2592000 && way < 15552000) {
        r = Math.floor(way / 2592000) + '个月前';
    } else if (time === 0 || !time) {
        r = '暂无';
    } else {
        r = dateChange('Y-m-d', time);
    }
    return r;
}
// chat部分
const chatSetLastContent = (item, callVideo) => {  // 类型判断
	let msgName = '[暂不支持的消息类型]';
	switch (item.type) {
		case 'text':
			msgName = item;
			break;
		case 'event':
			msgName = item.lastContent;
			break;
		case 'image':
			msgName = '[图片]';
			break;
		case 'voice':
			msgName = '[语音]';
			break;
		case 'video':
			msgName = '[视频]';
			break;
		case 'file':
			msgName = '[文件]';
			break;
		case 'emoticon':
			msgName = '[表情]';
			break;
		case 'system':
			msgName = '[系统通知]';
			break;
		case 'repost':
			msgName = '[聊天记录]';
			break;
		case 'webrtc':
			if (callVideo) {
				msgName = '[正在请求与您视频通话]';
			} else {
				msgName = '[正在请求与您语音通话]';
			}
			break;
	}
	return msgName;
}
const emoji = () => {
	let emojidata = [
		'😉', '😃', '😅', '🤡', '😊', '🤑', '😌', '😍', '👍', '👌', '😷', '😇', '😛', '😂', '😄', '😁',
		'🤣', '🙂', '🌘', '🌗', '🌖', '🌕', '🌔', '🌓', '🌒', '🌑', '🌚', '🥰', '😘', '🙃', '😆', '😀',
		'😗', '😝','🤓', '😏', '😒', '😎', '😙', '😚', '🤪', '😞', '😔', '🤩', '🤨', '😋', '🧐', '🥳', 
		'😟', '😏','😕', '😫', '😤', '😒', '🙁', '😩', '😠', '😡', '🥺', '😞', '😔', '😣', '😢', '🤬', 
		'🤯', '😭','😖', '😟', '😳', '🥵', '🥶', '😶‍️', '😱', '😨', '😰', '😥', '😓', '🤗', '🤔', '🤭', 
		'🤫', '🤥','😶', '😐', '😑', '😬', '🙄', '😯', '😦', '😧', '😮', '😲', '🥱', '😴', '🤤', '😪',
		'😮', '‍😵','‍🤐', '🥴', '🤢', '🤮', '🤧', '🤒', '🤕', '🤠', '⭐', '️🌟', '✨', '💫', '💨', '🤲',
		'👐', '🙌','👏', '🤝', '👎', '👊', '✊', '🤛', '🤜', '🤞', '🤟', '🤘', '🤏', '👈', '👉', '👆','👇', 
		'️✋', '🤚', '🖐', '️🖖', '👋', '🤙', '💪', '🙏'
	];
	return emojidata
}
// 音频房间号
const roomcode = (userId,youID,orderid) => {
	let arr = [userId, youID];
	arr.sort((a, b) => a - b);
	let roomId = arr[0] + '-' + arr[1]+'-'+orderid
	return roomId
}

// 生成chat_sign
const generate_chat_sign = (userId,toUserId,orderId) => {
	if(!orderId){
		if(userId < toUserId){
			return userId+'-'+ toUserId+'-'+0
		}else{
			return toUserId+'-'+ userId +'-'+0
		}
	}
	return userId +'-'+ toUserId +'-'+ orderId
}

// 替换换行
const textWithLineBreaks = (str) => {  
	return str.replace(/\n/g, "<br>");
}

// 正则匹配style以及所有值
const styleRegex = (str) => {
	return new RegExp(/style="([^"]*)"/g);
}

export default{
	base64toBlob:base64toBlob,
	beforeTime:beforeTime,
	chatSetLastContent:chatSetLastContent,
	emoji:emoji,
	roomcode:roomcode,
	generate_chat_sign:generate_chat_sign,
	textWithLineBreaks:textWithLineBreaks
}